<?php
/*
 <script type="text/javascript" src="http://www.google.com/reader/ui/publisher-de.js">
 </script>
 <script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/09391084871128771489/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22Empfehlungen%20von%20jaloma%22%2Cs%3A%22true%22%2Cn%3A%22false%22%2Cb%3A%22false%22%7D)%3Bnew%20GRC">
 </script>
 <script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/09391084871128771489/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22-%22%2Ct%3A%22Empfehlungen%20von%20jaloma%22%2Cs%3A%22true%22%2Cn%3A%22false%22%2Cb%3A%22false%22%7D)%3Bnew%20GRC"></script>
 <script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/09391084871128771489/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22-%22%2Ct%3A%22Empfehlungen%20von%20jaloma%22%2Cs%3A%22true%22%2Cn%3A%22true%22%2Cb%3A%22false%22%7D)%3Bnew%20GRC"></script>
 <script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/09391084871128771489/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22-%22%2Ct%3A%22Empfehlungen%20von%20jaloma%22%2Cs%3A%22false%22%2Cn%3A%22false%22%2Cb%3A%22false%22%7D)%3Bnew%20GRC"></script>*/
/**
 * Userfly Action Plugin:   Register UserFly-Javascript
 *
 * @author     Jürgen A.Lamers <jaloma.ac@googlemail.com>
 */

if (!defined('DOKU_INC'))define('DOKU_INC', realpath(dirname( __FILE__ ).'/../../').'/');
if (!defined('DOKU_PLUGIN'))define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
require_once (DOKU_PLUGIN.'syntax.php');

class syntax_plugin_grclip extends DokuWiki_Syntax_Plugin
{

    var $dflt = array (
    'user'=>'off',//'09391084871128771489',
    'items'=>'off',//5,
    'color'=>'off',//'-',//black, blue, gray, khaki, pink, slate, none, green
    'title'=>'off',//'Empfehlungen%20von%20jaloma',
    'quelle'=>'off',//'true',
    'annotation'=>'off',//'false',
    'showall' => 'off',
    'weite'=>'off'
    );

    /**
     * return some info
     */
    function getInfo()
    {
        return array (
        'author'=>'Jürgen A.Lamers',
        'email'=>'jaloma.ac@googlemail.com',
        'date'=>'2009-12-06',
        'name'=>'GoogleReaderClip (syntax plugin component)',
        'desc'=>'GoogleReaderClip syntax functions.',
        'url'=>'http://www.dokuwiki.org/plugin:googlereader_clip',
        );
    }

    function getType()
    {
        return 'substition';
    }
    function getPType()
    {
        return 'block';
    }
    function getSort()
    {
        return 900;
    }

    /**
     *  Pattern Matching?
     */
    function connectTo($mode)
    {
        $this->Lexer->addSpecialPattern('<grclip.*?/>', $mode, 'plugin_grclip');
    }

    function matchLength()
    {
        return strlen("<grclip");
    }

    function render($mode, & $renderer, $data)
    {
        global $conf;
        if ($mode == 'xhtml')
        {
            $user = $data['user'];
            if ($user == 'off')
            {
                $user = $this->getConf('user');
            }
            $items = $data['items'];
            if ($items == 'off')
            {
                $items = $this->getConf('items');
            }
            $color = $data['color'];
            if ($color == 'off')
            {
                $color = $this->getConf('color');
            }
            $title = $data['title'];
            if ($title == 'off')
            {
                $title = $this->getConf('title');
            }
            $quelle = $data['quelle'];
            if ($quelle == 'off')
            {
                $quelle = $this->getConf('gr_quelle');
                if ($quelle)
                {
                    $quelle = 'true';
                } else
                {
                    $quelle = 'false';
                }
            }
            $annot = $data['annotation'];
            if ($annot == 'off')
            {
                $annot = $this->getConf('gr_annotation');
                if ($annot)
                {
                    $annot = 'true';
                } else
                {
                    $annot = 'false';
                }
            }
			$showall = $data['showall'];
			if ($showall == 'off')
            {
                $showall = $this->getConf('gr_showall');
                if ($showall)
                {
                    $showall = 'true';
                } else
                {
                    $showall = 'false';
                }
            }
            $width = $data['weite'];

            $script = 'http://www.google.com/reader/public/javascript/user/'.$user.
            '/state/com.google/broadcast?n='.$items.
            '&callback=GRC_p(%7Bc%3A%22'.$color.
            '%22%2Ct%3A%22'.$title.
            '%22%2Cs%3A%22'.$quelle.
            '%22%2Cn%3A%22'.$annot.
            '%22%2Cb%3A%22'.$showall.
            '%22%7D)%3Bnew%20GRC';
            $script = '<script type="text/javascript" src="'.$script.'"></script>';
            if ($width != 'off')
            {
                $script = '<div style="width:'.$width.';">'.$script.'</div>';
            } else
            {
                $script = '<div>'.$script.'</div>';
            }
            $renderer->doc .= $script."";
        }
        return false;
    }
    /**
     *  Parsing
     *  1. Very rough parsing involved
     *  2. Applet parameters not included
     */
    function handle($match, $state, $pos, & $handler)
    {
        //   list($str_params) = explode('>',substr($match,8,-2),-1);
        $match = html_entity_decode(substr($match, $this->matchLength(), -2));
        $gmap = $this->_extract_params($match);
        return $gmap;
    }

    /**
     * extract parameters for the googlemap from the parameter string
     *
     * @param   string    $str_params   string of key="value" pairs
     * @return  array                   associative array of parameters key=>value
     */
    function _extract_params($str_params)
    {
        $param = array ();
        preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER);

        // parse match for instructions, break into key value pairs
        $gmap = $this->dflt;
        foreach ($param as $kvpair)
        {
            list ($match, $key, $val) = $kvpair;
            $key = strtolower($key);
            //        if (isset($gmap[$key])) $gmap[$key] = strtolower($val);
            if ( isset ($gmap[$key]))$gmap[$key] = $val;
        }
        return $gmap;
    }

} //class
